package com.leihao.marketing.controller;

import com.github.pagehelper.PageInfo;
import com.leihao.common.core.domain.R;
import com.leihao.marketing.domain.ListRequest;
import com.leihao.marketing.domain.Preference;
import com.leihao.marketing.service.PreferenceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("preference")
public class PreferenceController {
    @Autowired
    private PreferenceService preferenceService;


    /**
     * 添加优选
     * @param preference
     * @return
     */
    @PostMapping("add")
    @ApiOperation(value = "添加优选")
    public R add(@Validated @RequestBody Preference preference) {
        preferenceService.add(preference);
        return R.ok(null,"添加成功");
    }
    /**
     * 查询所有推荐优选
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation(value = "查询所有推荐优选")
    public R<List<Preference>> findAll() {
        return R.ok(preferenceService.findAll());
    }

    /**
     * 删除优选
     * @param preferenceId
     * @return
     */
    @DeleteMapping("delete/{preferenceId}")
    @ApiOperation(value = "删除优选")
    @ApiImplicitParam(name = "preferenceId",value = "优选编号",required = true,dataType = "Integer")
    public R delete(@PathVariable Integer preferenceId) {
        preferenceService.delete(preferenceId);
        return R.ok(null,"删除成功");
    }

    /**
     * 分页查询优选
     * @return
     */
    @PostMapping("findList")
    @ApiOperation(value = "分页查询优选")
    public R<PageInfo<Preference>> findList(@RequestBody ListRequest listRequest) {
        PageInfo<Preference> all = preferenceService.findList(listRequest);
        return R.ok(all,"查询成功");
    }

    /**
     * 根据ID查询优选
     * @param preferenceId
     * @return
     */
    @GetMapping("findById/{preferenceId}")
    @ApiOperation(value = "根据ID查询优选")
    @ApiImplicitParam(name = "preferenceId",value = "优选编号",required = true,dataType = "Integer")
    public R<Preference> findById(@PathVariable Integer preferenceId){
        return R.ok(preferenceService.findById(preferenceId),"查询成功");
    }

    /**
     * 修改优选
     * @param preference
     * @return
     */
    @PutMapping("update")
    @ApiOperation(value = "修改优选")
    public R update(@Validated @RequestBody Preference preference) {
        preferenceService.update(preference);
        return R.ok(null,"修改成功");
    }

}
